﻿
namespace SharpShare.Afp.Protocol.Security {
    public abstract class AfpUserAuthenticationResult {
        internal AfpUserAuthenticationResult() {
        }

        public abstract AfpResultCode Execute(IAfpSession session, AfpStream responseStream);
    }

    public sealed class AfpAllowUserAuthenticationResult : AfpUserAuthenticationResult {
        public AfpAllowUserAuthenticationResult(string version, string uam, IAfpUser user) {
            this.User = user;
            this.Version = version;
            this.Uam = uam;
        }

        public IAfpUser User { get; private set; }
        public string Version { get; private set; }
        public string Uam { get; private set; }

        public override AfpResultCode Execute(IAfpSession session, AfpStream responseStream) {
            session.Login(this.Version, this.Uam, this.User);

            return AfpResultCode.FPNoErr;
        }
    }

    public sealed class AfpDenyUserAuthenticationResult : AfpUserAuthenticationResult {
        public AfpDenyUserAuthenticationResult(string reason = null) {
            this.Reason = reason;
        }

        public string Reason { get; private set; }

        public override AfpResultCode Execute(IAfpSession session, AfpStream responseStream) {
            return AfpResultCode.FPUserNotAuth;
        }
    }

    public sealed class AfpContinueUserAuthenticationResult : AfpUserAuthenticationResult {
        public AfpContinueUserAuthenticationResult(short id, byte[] data) {
            this.Id = id;
            this.Data = data;
        }

        public short Id { get; private set; }
        public byte[] Data { get; private set; }

        public override AfpResultCode Execute(IAfpSession session, AfpStream responseStream) {
            responseStream.WriteInt16(this.Id);
            responseStream.WriteBytes(this.Data);

            return AfpResultCode.FPAuthContinue;
        }
    }
}
